""" Pig Latin Lexer if a string starts with a consonant, put the consonant at the end of the word and add the string "ay" to the end. >Consonant #before >Onsonantcay #after if a string starts with a vowel, just add "ay" to the end of the string. >apple #before >appleay #before written using SlyLex Yacc nick creel sp19 compiler design/programming languages """ from sly import Lexer class PigLexer(Lexer): #token names tokens = { CONSONANT, VOWEL } ignore = ' \t' #Regular expressions defining tokens CONSONANT = r'[b-zB-Z&&^eiouEIOU][A-Za-z]*' VOWEL = r'[aeiouAEIOU][a-zA-Z]*' def CONSONANT(self, t): ``` Test the first character in t.value. If t.value[0] is a consonant, set t.value equal to t.value[1:] + t.value[0] + 'ay' then return the token. >>> CONSONANT("Big") == "igBay" ``` t.value = t.value[1:] + t.value[0] + 'ay' return t def VOWEL(self, t): ``` test the first character in t.value. if t.value[0] is a vowel, then make t.value equal to t.value + 'ay', then return t. >>> VOWEL("apple") == "appleay" ``` t.value = t.value + 'ay' return t if __name__ == '__main__': string = "Big bad apple" lexer = PigLexer() result = "" for tok in lexer.tokenize(string): result = result + tok.value + " " print("Input: " + string) print("Result: " + result)